import * as dynamicSearchService from '../../services/dynamicSearch';
import {queryAssetType} from '../../services/assetType';
import {queryAssetBrand,queryAssetModelAll} from '../../services/assetModel';
import {queryRoomList} from '../../services/tRoom';
import {qryRackList} from '../../services/equipment';

export default {
  namespace: 'dynamicSearch',
  state: {
    list: [],
    assetTypeTree:[],
    roomList:[],
    assetBrandList:[],
    assetModelList:[],
    rackList:[],
    typeAttrGroup:[],
    existsSerialNo:false,
    current : 1,
    currentSize : 10,
    total : 0,
    modalVisible : false,
    modalType : 'create',
    newKey : '',
    groupList: [],
    attrValues:[],
    attrValuesForm:{},
    assetId:undefined,
    typeId:undefined,
    brandId:undefined,
    assetData:{},
    selectType:1,    
    searchRackList:[],
    searchModalShow:false,
  },
  reducers: {
    updateState(state, { payload }){
      return { ...state, ...payload };
    },
    queryListSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    checkSerialNoSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    queryBrandListSuc(state, { payload: assetBrandList }) {
      return { ...state, assetBrandList };
    },
    queryRoomListSuc(state, { payload: roomList }) {
      return { ...state, roomList };
    },
    queryAssetTypesSuc(state, { payload: assetTypeTree }) {
      return { ...state, assetTypeTree };
    },
    queryModelListSuc(state, { payload: assetModelList }) {
      return { ...state, assetModelList };
    },
    qryRackListSuc(state, { payload: rackList }) {
      return { ...state, rackList };
    },
    showModal (state, { payload }) {
        return { ...state, ...payload, newKey : new Date().getTime() + '' , modalVisible: true }
    },
    hideModal (state) {
      return { ...state, modalVisible: false, newKey : new Date().getTime() + '' }
    },
    searchRackListSuc(state, { payload: searchRackList }) {
      return { ...state, searchRackList };
    },

  },
  effects: {
    *queryAssetType({ payload },{ call, put }) {
      const { body }  = yield call(queryAssetType);
      yield put({ type: 'queryAssetTypesSuc', payload: body.data });
    },
    *queryRoom({ payload },{ call, put }) {
      const { body } = yield call(queryRoomList,payload);
      yield put({ type: 'queryRoomListSuc', payload: body.data });
    },
    *queryAssetBrand({ payload },{ call, put }) {
      const { body } = yield call(queryAssetBrand,payload);
      yield put({ type: 'queryBrandListSuc', payload: body.data });
    },
    *queryList({ payload },{ call, put }) {
      const { body } = yield call(dynamicSearchService.queryList,payload);
      yield put({ type: 'queryListSuc', payload: { list:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords}});
    },
    *queryAssetModel({ payload },{ call, put }) {
      const { body } = yield call(queryAssetModelAll,payload);
      yield put({ type: 'queryModelListSuc', payload: body.data });
    },
    *qryRackList({ payload },{ call, put }) {
      const { body }  = yield call(qryRackList,payload);
      yield put({ type: 'qryRackListSuc', payload:body.data});
    },

    *qryAttrByTypeId({ payload },{ call, put }) {
      const { body }  = yield call(dynamicSearchService.qryAttrByTypeId,payload);
      yield put({
        type: 'updateState',
        payload: {
          typeAttrGroup: body.data,
        },
    });
    },

    *qryAttrValue({ payload },{ call, put }){
      const { body } = yield call(dynamicSearchService.qryAttrValue,payload);
      if(body){
        var dat = {};
        body.attrValues?body.attrValues.map(d=>{
          dat[d.attrIdStr] = d.value;
        }):null
        yield put({
            type: 'updateState',
            payload: {
              groupList: body.data,
              attrValues:body.attrValues,
              attrValuesForm: dat,
            },
        });
      
      }
    },

    *qryById({ payload },{ call, put }){
      const { body } = yield call(dynamicSearchService.qryById,payload);
      if(body){
        yield put({
          type: 'showModal',
          payload: {
            assetData:body.data,
            modalType: 'edit',
            title: '资产配置',
          },
      });
      
      }
    },

    *qrySearchRackList({ payload },{ call, put }) {
      if(payload){
        const { body }  = yield call(qryRackList,payload);
        yield put({ type: 'searchRackListSuc', payload:body.data});
      }else{
        yield put({ type: 'searchRackListSuc', payload:[]});
      }     
    },

  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/asset/dynamicSearch') {
          dispatch({type: 'updateState',payload: {searchModalShow:false,},});
          dispatch({ type: 'queryAssetType' });
          dispatch({ type: 'queryAssetBrand' ,payload: {  }, });
          dispatch({ type: 'queryRoom' });
          dispatch({ type: 'queryList', payload:{ 'status':'' }});
        }
      });
    },
  },
};
